import Koa from "koa";
import logger from "koa-logger";
import bodyParser from "koa-bodyparser";
import serve from "koa-static";
import path from "path";
import compress from "koa-compress";
import cors from "koa2-cors";
import router from "./routes/index.js";
const app = new Koa();

app.use(cors({ origin: '*', credentials: true }));
// 压缩HTTP响应的内容, 减少传输数据量, 加快网页加载速度
app.use(compress());
// Logger 记录请求的日志记录
app.use(logger());
// 解析 post 接口请求体
app.use(bodyParser());
// 静态资源
const filename = import.meta.filename;
const currentDir = path.dirname(filename);
const staticPath = path.join(currentDir, 'public');
app.use(serve(staticPath));

// 使用路由
app.use(router.routes()).use(router.allowedMethods());

app.listen(3000, () => {
    console.log("Server is running on http://localhost:3000");
});
